package sistemaClassificador.algoritmoGenetico;

import java.util.Random;

public class Roleta {

	public Roleta() {

	}

	public int girar(Populacao p, MersenneTwister geradorRandom){
		int i=0;
		double aux    = 0;
		double total  = p.getTotalFitness();
		double x      = geradorRandom.nextDouble();
		double limite = Math.abs(x) * total;
	//	System.out.println("limite = "+limite+"| total = "+total+"| x"+x);
		for(i=0;((i<p.size())&&(aux<limite));++i){
			try {
				aux+= p.getRegra(i).getFitness();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(i==0) {
	//		System.out.println("limite = "+limite + " total = "+ total + " i = "+i);
			
			return i;
		}
		i--;
	//	System.out.println("limite = "+limite + " total = "+ total + " i = "+i);
		return i;
	}

}
